package com.niiwoo.civet.trade.service.local.loan.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.user.dto.request.AppButtonDTO;
import com.niiwoo.civet.user.dto.request.UserLetterRequestDTO;
import com.niiwoo.civet.user.enums.MessageTemplateKeyEnum;
import com.niiwoo.civet.user.enums.SMSTemplateKeyEnum;
import com.niiwoo.civet.user.enums.UserLetterTypeEnum;
import com.niiwoo.civet.user.service.SendSmsDubboService;
import com.niiwoo.civet.user.service.UserLetterDubboService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 二次获取授信额度发送站内信与短信
 * Created by HuangMin on 2018/5/24.
 */
@Slf4j
@Service
public class CreditAmountNotificationsService {

    @Reference(version = "1.0.0")
    private UserLetterDubboService userLetterDubboService;

    @Reference(version = "1.0.0")
    private SendSmsDubboService sendSmsDubboService;

    @Value("${h5-borrow-detail-url}")
    private String h5BorrowDetailUrl;


    /**
     * 发送站内信息与短信
     * 审核通过待确认
     *
     * @param project
     */
    public void auditConfirmationNotice(Project project) {
        //设置参数
        List<Object> smsTemplateParamList = new ArrayList<Object>();
        //发送短信服务
        sendSmsDubboService.sendSmsByUserId(project.getBorrowUserId(), SMSTemplateKeyEnum.SMS_USER_CONFIRM_PUSH_PROJECT.getKey(), smsTemplateParamList);

        //站内信
        UserLetterRequestDTO userLetterRequestDTO = new UserLetterRequestDTO();
        userLetterRequestDTO.setUserLetterTypeEnum(UserLetterTypeEnum.ONLY_LETTER);
        userLetterRequestDTO.setLetterTemplateEnum(MessageTemplateKeyEnum.LetterTemplateKeyEnum.LETTER_USER_CONFIRM_PUSH_PROJECT);

        //设置参数
        List<Object> letterTemplateParamList = new ArrayList<Object>();
        letterTemplateParamList.add(project.getProjectId());
        letterTemplateParamList.add(project.getTitle());

        //按钮列表
        List<AppButtonDTO> appButtonDTOS = new ArrayList<>();

        //查看借款
        AppButtonDTO appButtonDTO = new AppButtonDTO();
        appButtonDTO.setButtonType(AppButtonDTO.ButtonTypeEnum.WAIT_CONFIRM);
        appButtonDTO.setLabel(AppButtonDTO.ButtonTypeEnum.WAIT_CONFIRM.getDesc());

        // 钱小二
        AppButtonDTO appButtonDTO2 = new AppButtonDTO();
        appButtonDTO2.setButtonType(AppButtonDTO.ButtonTypeEnum.WAIT_CONFIRM);
        appButtonDTO2.setLabel(AppButtonDTO.ButtonTypeEnum.MONEY_2.getDesc());

        Map<String, Object> params = new HashMap<>();
        params.put("ProjectId", project.getProjectId());
        params.put("LoadServerUrl", h5BorrowDetailUrl + project.getProjectId());

        appButtonDTO.setParams(params);
        appButtonDTOS.add(appButtonDTO);
        appButtonDTOS.add(appButtonDTO2);

        userLetterRequestDTO.setLetterTemplateParamList(letterTemplateParamList);
        userLetterRequestDTO.setProjectId(project.getProjectId());
        userLetterRequestDTO.setToUserId(project.getBorrowUserId());
        userLetterRequestDTO.setLetterButtonList(appButtonDTOS);
        log.info("标准产品通过待确认userLetterRequestDTO:{}", JSON.toJSONString(userLetterRequestDTO));
        userLetterDubboService.pushLetter(userLetterRequestDTO);
    }


}
